package client;

import server.Sjdbc;
import java.sql.*;
import java.util.LinkedList;

public class Ccontrol {
    private static Ccontrol ctller=null;
    public static Ccontrol getCtller(){
        if(ctller==null)
            ctller=new Ccontrol();
        return ctller;
    }
    private static String act = "select * from ";//"insert into "; "update "; "delete "; "select * from ";
    private static String tabl = "";
    private static String condition="";//加在where后面的内容
    private Ccontrol(){

    }

    public void enterAttempt(String ipw) {
        String rspwd="";
        String info="";
        String command = this.stuEnter();
        if(command.equals("")){
            Cmodel.getMe().setIdentity("dba");
            LinkedList<String> origin=new LinkedList<>();
            origin.add("Welcome Our Adm!");
            Cmodel.getMe().setRst(origin);
            Cview.getMyfram().login2menu();
            return;
        }
        ResultSet rss = this.sendQueryCommand(command);

        try {
            rss.next();
            rspwd=rss.getString("stupwd");
            info="student:  "+rss.getString("stuId")+"    |    "+rss.getString("stuname")
                    +"    |    "+rss.getString("stumajor");
        } catch (SQLException e) {
            //throw new RuntimeException(e);
            command=teaEnter();
            rss = sendQueryCommand(command);
            try {
                rss.next();
                rspwd=rss.getString("teapwd");
                info="teacher:  "+rss.getString("teaId")+"    |    "+rss.getString("teaname")
                        +"    |    "+rss.getString("teacollege");
            } catch (SQLException e2) {
                //throw new RuntimeException(e);
                return;
            }
            if(rspwd.equals(ipw)){
                Cmodel.getMe().setIdentity("teacher");
                LinkedList<String> origin=new LinkedList<>();
                origin.add("teacher0:   teaId    |    teaname    |    teacollege");
                origin.add(info);
                Cmodel.getMe().setRst(origin);
                Cview.getMyfram().login2menu();
            }
        }
        //System.out.println(rspwd); //System.out.println(ipw);
        if(rspwd.equals(ipw)){
            Cmodel.getMe().setIdentity("student");
            LinkedList<String> origin=new LinkedList<>();
            origin.add("student0:   stuId    |    stuname    |    stumajor");
            origin.add(info);
            Cmodel.getMe().setRst(origin);
            Cview.getMyfram().login2menu();
        }
//        else{
//            command=teaEnter();
//            rss = sendCommand(command);
//
//            if(rss!=null){
//                try {
//                    rss.next();
//                    rspwd=rss.getString("teapwd");
//                    info="teacher: "+rss.getString("teaId")+" | "+rss.getString("teaname")
//                            +" | "+rss.getString("teacollege");
//                } catch (SQLException e) {
//                    throw new RuntimeException(e);
//                }
////                System.out.println(rspwd);
////                System.out.println(ipw);
//                if(rspwd.equals(ipw)){
//                    Cmodel.getMe().setIdentity("teacher");
//                    LinkedList<String> origin=new LinkedList<>();
//                    origin.add(info);
//                    Cmodel.getMe().setRst(origin);
//                    Cview.getMyfram().login2menu();
//                }
//            }
//        }
    }
    private String stuEnter(){
        if(Cmodel.getcId()==0){
            return "";
        }
        String command="select  *  from  student0  where  stuId="+Cmodel.getcId();
        return command;
    }
    private String teaEnter(){
        String command="select * from teacher0 where teaId="+Cmodel.getcId(); //System.out.println(command);//检查一下
        return command;
    }
    public void directCommand(String command){
        LinkedList<String> rslist=new LinkedList<>();
        System.out.println("Command is " + command);
        if(command.startsWith("select")){
            ResultSet rss = sendQueryCommand(command);
            try {
                this.accessResult(rss);
            } catch (SQLException e) {
                rslist.add("--------------------");
                rslist.add("----Failed2Access---");
                rslist.add("--------------------");
                //throw new RuntimeException(e);
            }
        }else if (command.startsWith("update")){
            int rss = sendUpdateCommand(command);
            switch (rss){
                case -1:rslist.add("Failed to Update...");break;
                default:rslist.add("Succeed tp Update! Result = " + rss);
            }
        }else{
            boolean rss = sendOtherCommand(command);
            if(rss){
                rslist.add("Failed");
            }else{
                rslist.add("Succeed");
            }
        }

        //Cmodel.getMe().setRst(rslist);
        Cview.getMyfram().login2menu();
    }//管理员直接查询时应当点击menu以确定查询类型，否则无法查询到结果
    private ResultSet sendQueryCommand(String command){
        ResultSet rss=null;
        Connection conn = Sjdbc.getMyjd().getConn();
        try {
            Statement stm=conn.createStatement();
            rss=stm.executeQuery(command);
        } catch (SQLException e) {
            System.out.println("--------------------");
            System.out.println("----Failed2Query----");
            System.out.println("--------------------");
            //throw new RuntimeException(e);
        }
        return rss;
    }
    private int sendUpdateCommand(String command){
        int rss=-1;
        Connection conn = Sjdbc.getMyjd().getConn();
        try {
            Statement stm=conn.createStatement();
            rss=stm.executeUpdate(command);
        } catch (SQLException e) {
            System.out.println("--------------------");
            System.out.println("----Failed2Update---");
            System.out.println("--------------------");
            //throw new RuntimeException(e);
        }
        return rss;
    }
    private boolean sendOtherCommand(String command){
        boolean rss=false;
        Connection conn = Sjdbc.getMyjd().getConn();
        try {
            Statement stm=conn.createStatement();
            rss=stm.execute(command);
        } catch (SQLException e) {
            System.out.println("--------------------");
            System.out.println("----Failed2Other----");
            System.out.println("--------------------");
            //throw new RuntimeException(e);
        }
        return rss;
    }
    private String composeCommand(){
        if(tabl.equals(""))
            return "";//如果tabl为空则不构造任何命令
        String command="";
        //String condition = "";// " stuId=2212537";//TODO 编写此字段的来历
        String iden="";
        if(Cmodel.getMe().getIdentity().equals("student")) iden=" stuId=";
        else iden=" teaId=";
        System.out.println(act);

        if(act.equals("select * from ")){
            String backend = "";//后缀--也就是where...
            if(!condition.equals(""))
                backend = " where " + condition;
            command = act + tabl + backend;
        }
        else if(act.equals("update ")){
            command = act + tabl + " set " + condition + " where " + iden + Cmodel.getcId();
        }
        else{
            command = "select * from " + tabl;
        }//能够正常选路

        System.out.println(command);
        return command;
    }
    private void accessResult(ResultSet irs) throws SQLException {
        LinkedList<String> rslist=new LinkedList<>();
        System.out.println(tabl);//TODO
        if(tabl.equals("college0 ")){
            //rslist.add("student0:   stuId    |    stuname    |
            rslist.add("college0:     clgname     |     clgmaster");
            for(;irs.next();){
                //rslist.add(irs.get); TODO 获取什么需要设计定性
                rslist.add(irs.getString("clgname") + "    |    " + irs.getInt("clgmaster"));
            }
        }else if(tabl.equals("major0 ")){
            rslist.add("major0:    mjrId    |     mjrname     |    affcollege");
            System.out.println("THIS IS MAJOR0");
            for(;irs.next();){
                rslist.add(irs.getInt("mjrId") + "    |    " + irs.getString("mjrname")
                    + "     |     " + irs.getString("affcollege"));
            }
        }else if(tabl.equals("student0 ")){
            rslist.add("student0:    stuId    |     stuname     |    stumajor");
            for(;irs.next();){
                rslist.add(irs.getInt("stuId") + "    |    " + irs.getString("stuname")
                        + "     |     " + irs.getInt("stumajor"));
            }
        }else if(tabl.equals("teacher0 ")){
            rslist.add("teacher0:    teaId    |     teaname     |    teacollege");
            for(;irs.next();){
                rslist.add(irs.getInt("teaId") + "    |    " + irs.getString("teaname")
                        + "     |     " + irs.getString("teacollege"));
            }
        }else if(tabl.equals("course0 ")){
            rslist.add("course0:    couId    |     couname     |    teaId");
            for(;irs.next();){
                rslist.add(irs.getInt("couId") + "    |    " + irs.getString("couname")
                        + "     |     " + irs.getInt("couteaId"));
            }
        }else if(tabl.equals("attendclass0 ")){
            rslist.add("attendclass0:    stuId    |     couId     |    grade");
            for(;irs.next();){
                rslist.add(irs.getInt("stuId") + "    |    " + irs.getInt("couId")
                        + "     |     " + irs.getInt("grade"));
            }
        }
        Cmodel.getMe().setRst(rslist);
    }
    public void execute() {
        String command = this.composeCommand();
        //执行sql命令
        if(act.equals("select * from ")){
            ResultSet rss = sendQueryCommand(command);
            try {
                this.accessResult(rss);
            } catch (SQLException e) {
                System.out.println("--------------------");
                System.out.println("----Failed2Access---");
                System.out.println("--------------------");
                //throw new RuntimeException(e);
            }
        }else if(act.equals("update ")){
            int rss = sendUpdateCommand(command);
        }
//        System.out.println("INFOS:");
//        for(int i=0;i<Cmodel.getMe().getRst().size();i+=1){
//            System.out.println(Cmodel.getMe().getRst().get(i));
//        }
        Cview.getMyfram().login2menu();
    }
    public void setAct(String toAct) {
//        if(toAct!="insert into " && toAct!="update " && toAct!="delete " && toAct!="select * ")
//            return;
        act=toAct;
        System.out.println("Set Act as: "+act);
    }
    public void settabl(String itable){
        tabl=itable;
        System.out.println("Set table as: "+tabl);
    }
    public void setCondition(String icdt){
        condition = icdt;
        System.out.println(condition);
    }
}
